clear 
set more off


forvalues i = 7/10 {
forvalues j = 1/4 {
if `i' <10 {
use "C:\Users\wevans1\Google Drive\opioids\florida_data\inpatient\stata_data_files\Inp0`i'Q`j'.dta"
}
else {
use  "C:\Users\wevans1\Google Drive\opioids\florida_data\inpatient\stata_data_files\Inp`i'Q`j'.dta"
}

gen opp=prindiag=="965.00" | prindiag=="956.09" | prindiag=="965.02"
gen op1=OTHDIAG1=="965.00" | OTHDIAG1=="956.09" | OTHDIAG1=="965.02"
gen op2=OTHDIAG2=="965.00" | OTHDIAG2=="956.09" | OTHDIAG2=="965.02"
gen op3=OTHDIAG3=="965.00" | OTHDIAG3=="956.09" | OTHDIAG3=="965.02"
gen op4=OTHDIAG4=="965.00" | OTHDIAG4=="956.09" | OTHDIAG4=="965.02"
gen op5=OTHDIAG5=="965.00" | OTHDIAG5=="956.09" | OTHDIAG5=="965.02"
gen op6=OTHDIAG6=="965.00" | OTHDIAG6=="956.09" | OTHDIAG6=="965.02"
gen op7=OTHDIAG7=="965.00" | OTHDIAG7=="956.09" | OTHDIAG7=="965.02"
gen op8=OTHDIAG8=="965.00" | OTHDIAG8=="956.09" | OTHDIAG8=="965.02"
gen op9=OTHDIAG9=="965.00" | OTHDIAG9=="956.09" | OTHDIAG9=="965.02"
gen op10=OTHDIAG10=="965.00" | OTHDIAG10=="956.09" | OTHDIAG10=="965.02"
gen op11=OTHDIAG11=="965.00" | OTHDIAG11=="956.09" | OTHDIAG11=="965.02"
gen op12=OTHDIAG12=="965.00" | OTHDIAG12=="956.09" | OTHDIAG12=="965.02"
gen op13=OTHDIAG13=="965.00" | OTHDIAG13=="956.09" | OTHDIAG13=="965.02"
gen op14=OTHDIAG14=="965.00" | OTHDIAG14=="956.09" | OTHDIAG14=="965.02"
gen op15=OTHDIAG15=="965.00" | OTHDIAG15=="956.09" | OTHDIAG15=="965.02"
gen op16=OTHDIAG16=="965.00" | OTHDIAG16=="956.09" | OTHDIAG16=="965.02"
gen op17=OTHDIAG17=="965.00" | OTHDIAG17=="956.09" | OTHDIAG17=="965.02"
gen op18=OTHDIAG18=="965.00" | OTHDIAG18=="956.09" | OTHDIAG18=="965.02"
gen op19=OTHDIAG19=="965.00" | OTHDIAG19=="956.09" | OTHDIAG19=="965.02"
gen op20=OTHDIAG20=="965.00" | OTHDIAG20=="956.09" | OTHDIAG20=="965.02"
gen op21=OTHDIAG21=="965.00" | OTHDIAG21=="956.09" | OTHDIAG21=="965.02"
gen op22=OTHDIAG22=="965.00" | OTHDIAG22=="956.09" | OTHDIAG22=="965.02"
gen op23=OTHDIAG23=="965.00" | OTHDIAG23=="956.09" | OTHDIAG23=="965.02"
gen op24=OTHDIAG24=="965.00" | OTHDIAG24=="956.09" | OTHDIAG24=="965.02"
gen op25=OTHDIAG25=="965.00" | OTHDIAG25=="956.09" | OTHDIAG25=="965.02"
gen op26=OTHDIAG26=="965.00" | OTHDIAG26=="956.09" | OTHDIAG26=="965.02"
gen op27=OTHDIAG27=="965.00" | OTHDIAG27=="956.09" | OTHDIAG27=="965.02"
gen op28=OTHDIAG28=="965.00" | OTHDIAG28=="956.09" | OTHDIAG28=="965.02"
gen op29=OTHDIAG29=="965.00" | OTHDIAG29=="956.09" | OTHDIAG29=="965.02"
gen op30=OTHDIAG30=="965.00" | OTHDIAG30=="956.09" | OTHDIAG30=="965.02"


gen ec1=ECODE1=="E850.2" | ECODE1=="E850.1"
gen ec2=ECODE2=="E850.2" | ECODE2=="E850.1"
gen ec3=ECODE3=="E850.2" | ECODE3=="E850.1"
gen opioid_admit=max(opp,op1,op2,op3,op4,op5,op6,op7,op8,op9,op10,op11,op12,op13,op14,op15,op16,op17,op18,op19,op20,op21,op22,op23,op24,op25,op26,op27,op28,op29,op30,ec1,ec2,ec3)
tab opioid_admit
gen nonopioid_admit=1-opioid_admit
drop if ptstate == "XX" | ptstate == "FL"
keep year age qtr FAC_COUNTY ptstate opioid_admit nonopioid_admit
save inp_admits`i'q`j', replace
}
}
forvalues i=7/9 {
forvalues j = 1/4 {
if `i' ==6 & `j' ==1 {
use inp_admits`i'q`j'
}
else {
append using inp_admits`i'q`j'
}

 }
 }

append using inp_admits10q1
append using inp_admits10q2

keep if age>=15 & age<=64
sort ptstate
collapse (sum) opioid_admit nonopioid_admit, by(ptstate)

sort ptstate
merge m:1 ptstate using state_fips_crosswalk
drop if _merge~=3
drop if fips==.
drop _merge
sort fips
merge m:1 fips using state_pop_2008
sort fips
keep if _merge==3
drop _merge
outsheet using inp_admits_2.csv, comma replace
